/**
 * @FileName: Loger.java
 * @Author
 * @Description:
 * @Date 2016年3月30日 上午11:38:51
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.service.ds;

import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DsLog{
    private static final String   LOG_PATH = "/log";

    private volatile static DsLog instance;
    private FileWriter            fr       = null;

    private DsLog(){
        try{
            String dirPath = Common.getRootDir() + LOG_PATH;
            Common.checkDir(dirPath);
            String filePath = String.format("%s/log%s.txt", dirPath,
                                            new SimpleDateFormat("_yyyyMMdd").format(new Date()));
            fr = new FileWriter(filePath, true);
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }

    private static DsLog getInstance(){
        if(instance == null){
            synchronized(DsLog.class){
                if(instance == null){
                    instance = new DsLog();
                }
            }
        }
        return instance;
    }

    public static void log(String log){
        getInstance().writeLog(log);
    }

    private synchronized void writeLog(String result){
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss.SSS");
            String date = sdf.format(new Date());
            fr.append(date);
            fr.append("--ThreadId--");
            fr.append("" + Thread.currentThread().getId());
            fr.append(":");
            fr.append(result);
            fr.append("\r\n");
            fr.flush();
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }
}
